#include "stdafx.h"
#include "AngelMemoryManager.h"

DECLARE_SINGLETON_CPP(AngelMemoryManager);

AngelMemoryManager::AngelMemoryManager(void)
{
}

AngelMemoryManager::~AngelMemoryManager(void)
{
	Blocks::iterator end = blocks.end();
	for (Blocks::iterator i = blocks.begin(); i != end; ++i)
	{
		free(i->block);
	}
}

void* AngelMemoryManager::Allocate(U32 _size)
{
	Blocks::iterator end = blocks.end();
	for (Blocks::iterator i = blocks.begin(); i != end; ++i)
	{
		if (i->size == _size)
		{
			void* mem = i->block;
			blocks.erase(i);
			return mem;
		}
	}
	return malloc(_size);
}

void AngelMemoryManager::Free(void* _memory, U32 _size)
{
	blocks.push_back(MemoryBlock(_memory, _size));
}
